//
//  MainViewController.swift
//  testZD
//
//  Created by 湛亚磊 on 2020/9/21.
//  Copyright © 2020 湛亚磊. All rights reserved.
//

import UIKit
import ESTabBarController_swift

open class BaseMainViewController: ESTabBarController {
    open override func viewDidLoad() {
        super.viewDidLoad()
        setupTabbarStyle()
        addChildControllers()
    }
    private func setupTabbarStyle()  {
        self.title = "Irregularity"
        self.tabBar.shadowImage = UIImage(named: "transparent")
        
        //设置选中字体颜色
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.RGB(r: 14, g: 185, b: 141)!], for: UIControl.State.selected)
        //设置未选中状态下字体颜色
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.RGB(r: 152, g: 154, b: 162)!], for: UIControl.State.normal)
        
        //因为tabbar有默认的渲染色，所以要先去掉，否则颜色并不是我们想要的颜色
        UITabBar.appearance().isTranslucent = false
        //之后再设置tabbar的背景色
        UITabBar.appearance().barTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
         //下面这个方法可以取消掉htabbar上的灰色线 两个都要写上，缺一不可
               
        UITabBar.appearance().shadowImage=UIImage.image(withColor: UIColor.white, size: CGSize(width: UIScreen.main.bounds.size.width, height: 1))

        UITabBar.appearance().backgroundImage = UIImage.image(withColor: UIColor.white, size: CGSize(width: ScreenW, height: TABBAR_HEIGHT))
        ///默认选中
        self.shouldHijackHandler = {
            tabbarController,viewController,index in
            
            return false
        }
        ///tabbar 点击动态 防止循环引用
        self.didHijackHandler = { tabbarController, viewController, index in
        }
        self.tabBar.barTintColor = UIColor.red
    }
    ///添加 自控制器 需要重写该方法
    open func addChildControllers()  {
    }
    
    //设置 控制器
    public func setChildViewController(_ childController:UIViewController,title:String,imageName:String)  {
        setAnimatedChildController(controller: childController, imageName: imageName, with: title)
        addChild(BaseNavigationController(rootViewController: childController))
    }
    public func setNormalTabbar(controller:UIViewController,with title:String,imageName:String)  {
        controller.tabBarItem.image = UIImage(named: imageName)
        controller.tabBarItem.title=title
        controller.tabBarItem.selectedImage = UIImage(named: imageName + "select")
    }
    
    private func setAnimatedChildController(controller:UIViewController,imageName:String,with title:String)  {
        controller.tabBarItem = ESTabBarItem.init(LBFMIrregularityBasicContentView(), title: title, image: UIImage(named: imageName), selectedImage: UIImage(named: imageName+"select"))
    }
}
